#!/bin/bash

set -e

# Issue - https://github.com/docker/hub-feedback/issues/384#issuecomment-214508878
# Docker Hub emits an invalid .dockercfg.  Remove the outer auths element.
if [[ $DOCKERCFG == {\"auths\":* ]]; then
    jsonStartIndex=$(expr index "$DOCKERCFG" :)
    length=$((${#DOCKERCFG} - $jsonStartIndex - 1))
    echo ${DOCKERCFG:jsonStartIndex:length} > /root/.dockercfg
fi


echo "Pushing latest image"
tagStartIndex=$(expr index "$IMAGE_NAME" :)
repoName=${IMAGE_NAME:0:tagStartIndex - 1}
latestImageName=$repoName":latest"
docker tag $IMAGE_NAME $latestImageName
docker push $latestImageName


echo "Pushing onbuild images"
versionedImageName=$repoName":1.0.0-preview2-onbuild"
docker tag onbuild $versionedImageName
docker push $versionedImageName

latestImageName=$repoName":onbuild"
docker tag onbuild $latestImageName
docker push $latestImageName
